package com.monkeygroup.mygoplus.common.config.updateapp;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.app.NotificationCompat;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.monkeygroup.mygoplus.R;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;

import java.io.File;
import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by LCW on 2017/7/15.
 */

public class DownloadService extends Service {
    private String mDownloadUrl;//APK的下载路径
    private NotificationManager mNotificationManager;
    private Notification mNotification;
    private static File localdir;
    private String apkName;

    @Override
    public void onCreate() {
        super.onCreate();
        mNotificationManager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
        localdir = new File(Environment.getExternalStorageDirectory(), "Download");
        if(!localdir.exists()){
            localdir.mkdir();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent == null) {
            notifyMsg("温馨提醒", "文件下载失败", 0);
            stopSelf();
        }
        mDownloadUrl = intent.getStringExtra("apkUrl");//获取下载APK的链接
        downloadFile(mDownloadUrl);//下载APK
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void notifyMsg(String title, String content, int progress) {

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);//为了向下兼容，这里采用了v7包下的NotificationCompat来构造
        builder.setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)).setContentTitle(title);
        if (progress > 0 && progress < 100) {
            //下载进行中
            builder.setProgress(100, progress, false);
        } else {
            builder.setProgress(0, 0, false);
        }
        builder.setAutoCancel(true);
        builder.setWhen(System.currentTimeMillis());
        builder.setContentText(content);
        if (progress >= 100) {
            //下载完成
            builder.setContentIntent(getInstallIntent());
        }
        mNotification = builder.build();
        mNotificationManager.notify(0, mNotification);
    }

    private PendingIntent getInstallIntent() {
        File file = new File(localdir + getFileName(mDownloadUrl));
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.parse("file://" + file.getAbsolutePath()), "application/vnd.android.package-archive");
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        return pendingIntent;
    }

    private void downloadFile(String url) {

        OkHttpUtils.get().url(url).build().execute(new FileCallBack(localdir.toString(), getFileName(mDownloadUrl)) {
            @Override
            public void onError(Call call, Exception e, int id) {
                notifyMsg("温馨提醒", "文件下载失败", 0);
                stopSelf();
            }

            @Override
            public void onResponse(File response, int id) {
                //当文件下载完成后回调
                notifyMsg("温馨提醒", "文件下载已完成", 100);
                stopSelf();
            }

            @Override
            public void inProgress(float progress, long total, int id) {
                //progress*100为当前文件下载进度，total为文件大小
                if ((int) (progress * 100) % 10 == 0) {
                    //避免频繁刷新View，这里设置每下载10%提醒更新一次进度
                    notifyMsg("温馨提醒", "文件正在下载..", (int) (progress * 100));
                }
            }
        });
    }

    private String getFileName(String url){
        try {
            Response response = OkHttpUtils.get().url(url).build().execute();
            Gson gson = new Gson();
            RequestModel model = gson.fromJson(gson.toJson(response.body()), new TypeToken<RequestModel>() {}.getType());
            apkName = (String) model.getData().get("path");
        } catch (IOException e) {
            e.printStackTrace();
        }

        return apkName;
    }
}
